package com.webcamtracker.ui;


import com.webcamtracker.color.segmentation.SegmentationImageTracker;
import com.webcamtracker.color.segmentation.Tracker;
import com.webcamtracker.move.tracking.ListenersLoader;
import com.webcamtracker.ui.target.UITargetModel;
import com.webcamtracker.ui.utils.WindowUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UITracker
        extends JFrame {
    //~ Instance fields ......................................................................................

    private JPanel controls;

    private UIVideoMonitor video;
    private UIDrawableCanvas workingArea;
    private static final Dimension CANVAS_SIZE = new Dimension(320, 240);

    //~ Constructors .........................................................................................

    public UITracker()
            throws HeadlessException {
        super("JTracker");
        configureUI();
    }

    //~ Methods ..............................................................................................

    public static void main(String[] args) throws InterruptedException {
        SplashBitmap splash = new SplashBitmap("tracker/img/TrackIngLogo.png");
		splash.hideOnClick();
        splash.hideOnTimeout(3000);
        splash.addManualProgressBar(0,3);
        splash.setProgressBarString("Loading TackIng...");
        splash.setProgressBarValue(1);
        splash.showSplash();
//        Thread.sleep(20);

        splash.setProgressBarString("Loading Plugins...");
        splash.setProgressBarValue(2);
        ListenersLoader.loadListeners();
        splash.setProgressBarString("Loading Libraries...");
//        Thread.sleep(20);
        UITracker colorTracker = new UITracker();
        WindowUtilities.setJavaLookAndFeel();
        colorTracker.setSize(new Dimension(950, 700));
        colorTracker.setLocationRelativeTo(null);
        colorTracker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        splash.setProgressBarValue(3);
        colorTracker.setVisible(true);

    }

    public void configureUI() {
        Container container = getContentPane();
        JPanel north = new JPanel(new BorderLayout());
        workingArea = new UIDrawableCanvas(CANVAS_SIZE);
        video = new UIVideoMonitor(workingArea);
        FramesPerSecond fps = new FramesPerSecond();
        Tracker videoSuscriptor = new SegmentationImageTracker();
        Tracker videoSuscriptor1 = new SegmentationImageTracker();
        Tracker videoSuscriptor2 = new SegmentationImageTracker();
        video.addVideoSuscriptor(videoSuscriptor);
        video.addVideoSuscriptor(videoSuscriptor1);
        video.addVideoSuscriptor(videoSuscriptor2);
        video.addVideoSuscriptor(workingArea);
        video.addVideoSuscriptor(fps);

        controls = new UITrackingOperations(this);

        JPanel workingPanel = new JPanel();
        workingPanel.setLayout(new BorderLayout());
        workingPanel.add(workingArea, BorderLayout.CENTER);
        workingPanel.add(fps, BorderLayout.SOUTH);

        JPanel panel = new JPanel(new GridLayout(1, 2));
        panel.add(getVideo());
        panel.add(WindowUtilities.titledComponent("Working Area", workingPanel));

        JTabbedPane properties = new JTabbedPane();
        loadSetupPropertiesPanels(properties, videoSuscriptor, videoSuscriptor1, videoSuscriptor2);

        north.add(panel, BorderLayout.CENTER);
        north.add(WindowUtilities.titledComponent("Operations", controls), BorderLayout.WEST);
        JComponent south = WindowUtilities.titledComponent("Tracking Options", properties);
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, north, south);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(0.5D);

        container.add(splitPane);


        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                video.stopVideo();
                System.exit(0);
            }
        });
    }


    public UIDrawableCanvas getWorkingArea() {
        return workingArea;
    }

    public UIVideoMonitor getVideo() {
        return video;
    }

    public JPanel getControls() {
        return controls;
    }


    protected void loadSetupPropertiesPanels(JTabbedPane controls, Tracker... videoSuscriptor) {
        for (int i = 0; i < videoSuscriptor.length; i++) {
            Tracker tracker = videoSuscriptor[i];
            controls.add("Object " + i, new UITargetModel(this, tracker));
        }

    }
}
